package com.cj.bus.common.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration // 该注解就是告诉Spring这个是一个配置文件类，这里配置的Bean要交给Spring去管理。这个就是用来取代Beans.xml这种文件的。
@EnableSwagger2
//@Profile({SpringContextUtil.DEV_PROFILE, SpringContextUtil.TEST_PROFILE})
public class SwaggerConfig {

  public final static String HEADER_SIGN = "BONC-SIGN";
  public final static String HEADER_ACCESS_KEY = "BONC-ACCESS-KEY";
  public final static String HEADER_TIME = "BONC-TIMESTAMP";

  @Bean
  public Docket createRestApi() {
    ParameterBuilder tokenPar = new ParameterBuilder();
    List<Parameter> pars = new ArrayList<Parameter>();
    tokenPar.name("token").description("访问令牌，在需要鉴权的接口，必须添加该参数").modelRef(new ModelRef("string"))
        .parameterType("header").required(false).build();
    pars.add(tokenPar.build());


    tokenPar = new ParameterBuilder();
    tokenPar.name(SwaggerConfig.HEADER_ACCESS_KEY).description("对外开放商户ACCESS_KEY").modelRef(new ModelRef("string"))
            .parameterType("header").required(false).build();
    pars.add(tokenPar.build());

    tokenPar = new ParameterBuilder();
    tokenPar.name(SwaggerConfig.HEADER_TIME).description("请求时间").modelRef(new ModelRef("string"))
            .parameterType("header").required(false).build();
    pars.add(tokenPar.build());

    tokenPar = new ParameterBuilder();
    tokenPar.name(SwaggerConfig.HEADER_SIGN).description("签名串").modelRef(new ModelRef("string"))
            .parameterType("header").required(false).build();
    pars.add(tokenPar.build());

    return new Docket(DocumentationType.SWAGGER_2).genericModelSubstitutes(DeferredResult.class)
        .useDefaultResponseMessages(false).forCodeGeneration(true).pathMapping("/")// base，最终调用接口后会和paths拼接在一起
        .select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .paths(PathSelectors.any()) // 对所有路径进行监控
        .build().globalOperationParameters(pars).apiInfo(apiInfo());
  }


  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("BONC-api文档")
        .description("湖南移动法务平台！")
        .contact(new Contact("ToC", "https://git.vankeservice.com/ZCFW/newborn.git", "yangh73@vanke.com")).version("1.0")
        .build();
  }
}
